//请实现一个函数，把字符串 s 中的每个空格替换成"%20"。 
//
// 
//
// 示例 1： 
//
// 输入：s = "We are happy."
//输出："We%20are%20happy." 
//
// 
//
// 限制： 
//
// 0 <= s 的长度 <= 10000 
//
// Related Topics 字符串 👍 440 👎 0


package leetcode.editor.cn;

// [剑指 Offer 05]替换空格

public class TiHuanKongGeLcof_JianZhiOffer05 {
    public static void main(String[] args) {
        Solution solution = new TiHuanKongGeLcof_JianZhiOffer05().new Solution();
        String ans = solution.replaceSpace("We are happy.");
        System.out.println(ans);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String replaceSpace(String s) {
            if (s == null) return null;
            int length = s.length();
            int size = 0;
            char[] charArr = new char[3 * length];
            for (int i = 0; i < length; i++) {
                if (s.charAt(i) == ' ') {
                    charArr[size++] = '%';
                    charArr[size++] = '2';
                    charArr[size++] = '0';
                } else {
                    charArr[size++] = s.charAt(i);
                }
            }
            return new String(charArr, 0, size);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

    //偷懒法
    class Solution2 {
        public String replaceSpace(String s) {
            if (s == null) return null;
            return s.replace(" ", "%20");
        }
    }
}